import 'package:flutter/material.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/schooladmin.dart';
import 'package:ichinesereader/controller/user.dart';
import 'package:ichinesereader/models/common/parentLetterResult.dart';
import 'package:ichinesereader/models/common/usernameList.dart';
import 'package:ichinesereader/service/schooladmin.dart';
import 'package:ichinesereader/utils/helper.dart';
import 'package:ichinesereader/widgets/tools/pdfviewer.dart';
import 'package:ichinesereader/themes/appTheme.dart';

class FloatingActionButtonStudentManagement extends StatefulWidget {
  FloatingActionButtonStudentManagement({
    Key? key,
  }) : super(key: key);

  @override
  State<FloatingActionButtonStudentManagement> createState() =>
      _FloatingActionButtonStudentManagementState();
}

class _FloatingActionButtonStudentManagementState
    extends State<FloatingActionButtonStudentManagement> {
  final box = Hive.box('appconfig');
  final SchoolAdminController _schoolAdminController = Get.find();
  final UserController _userController = Get.find();
  final SchoolAdminServiceProvider _adminServiceProvider =
      new SchoolAdminServiceProvider();

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    bool isSchoolAdmin = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_SchoolAdmin;

    return SpeedDial(
      animatedIcon: AnimatedIcons.menu_close,
      //openCloseDial: isDialOpen,
      backgroundColor:
          Get.isDarkMode ? Get.theme.primaryColorDark : AppTheme.colors.pinkBtn,
      foregroundColor:
          Get.isDarkMode ? Colors.black : AppTheme.colors.lightPink,
      overlayColor: Colors.grey,
      overlayOpacity: 0.6,
      spacing: 6,
      spaceBetweenChildren: 6,
      closeManually: false,
      children: [
        SpeedDialChild(
            child: Icon(
              Icons.disabled_by_default_rounded,
              color: AppTheme.colors.disableBtnLight,
            ),
            label: 'Disable Incentive ALL'.tr,
            labelStyle: TextStyle(
                color:
                    Get.isDarkMode ? Get.theme.primaryColorDark : Colors.black),
            backgroundColor: AppTheme.colors.disableBtn,
            onTap: () {
              String _classId = '';
              if (isSchoolAdmin) {
                _classId = _schoolAdminController
                    .currentSchoolAdminClass.classId
                    .toString();
                _schoolAdminController.updateClassIncentive(_classId, true);
              } else {
                _classId =
                    _userController.currentClass.value.classId.toString();
                _schoolAdminController.updateClassIncentive(_classId, true);
              }
            }),
        SpeedDialChild(
            child: Icon(
              Icons.approval_rounded,
              color: AppTheme.colors.primary,
            ),
            label: 'Enable Incentive ALL'.tr,
            labelStyle: TextStyle(
                color:
                    Get.isDarkMode ? Get.theme.primaryColorDark : Colors.black),
            backgroundColor: AppTheme.colors.textPrimaryDark,
            onTap: () {
              String _classId = '';
              if (isSchoolAdmin) {
                _classId = _schoolAdminController
                    .currentSchoolAdminClass.classId
                    .toString();
                _schoolAdminController.updateClassIncentive(_classId, false);
              } else {
                _classId =
                    _userController.currentClass.value.classId.toString();
                _schoolAdminController.updateClassIncentive(_classId, false);
              }
            }),
        SpeedDialChild(
            child: Icon(
              Icons.print_rounded,
              color: AppTheme.colors.babyBlue,
            ),
            label: 'Print Login Cards'.tr,
            labelStyle: TextStyle(
                color:
                    Get.isDarkMode ? Get.theme.primaryColorDark : Colors.black),
            backgroundColor: AppTheme.colors.purpleBtn,
            onTap: () async {
              String _classId = '';
              if (isSchoolAdmin) {
                _classId = _schoolAdminController
                    .currentSchoolAdminClass.classId
                    .toString();
              } else {
                _classId =
                    _userController.currentClass.value.classId.toString();
              }
              if (_schoolAdminController.studentItems.isNotEmpty &&
                  _classId != 'All') {
                var response =
                    await _adminServiceProvider.createClassLoginCards(_classId);
                if (response.statusCode == 200) {
                  ParentLetterResult parentletter =
                      parentLetterResultFromJson(response.bodyString);
                  if (parentletter.pdfUrl != null &&
                      parentletter.pdfUrl != '') {
                    if (GetPlatform.isWeb || GetPlatform.isDesktop)
                      launchInWebViewWithJavaScript(
                          parentletter.pdfUrl.toString());
                    else
                      Get.to(() => PDFViewer(
                          pdfaddress: parentletter.pdfUrl.toString()));
                  } else {
                    showSnackMsg('Failed to print class login cards');
                    // Get.snackbar(
                    //     'Message'.tr, 'Failed to print class login cards'.tr,
                    //     backgroundColor: Theme.of(context).primaryColor,
                    //     colorText: Theme.of(context).primaryColorLight,
                    //     snackPosition: SnackPosition.BOTTOM);
                  }
                } else {
                  showSnackMsg('Failed to print class login cards');
                  // Get.snackbar(
                  //     'Message'.tr, 'Failed to print class login cards'.tr,
                  //     backgroundColor: Theme.of(context).primaryColor,
                  //     colorText: Theme.of(context).primaryColorLight,
                  //     snackPosition: SnackPosition.BOTTOM);
                }
              }
            }),
        SpeedDialChild(
            backgroundColor: AppTheme.colors.pinkText,
            child: Icon(
              Icons.print_rounded,
              color: AppTheme.colors.orangeBtn,
            ),
            label: 'Print Parent Letter'.tr,
            labelStyle: TextStyle(
                color:
                    Get.isDarkMode ? Get.theme.primaryColorDark : Colors.black),
            onTap: () async {
              if (_schoolAdminController.studentItems.isNotEmpty &&
                  _schoolAdminController.studentItemsSelected.isNotEmpty) {
                UsernameList dto = new UsernameList();
                dto.userNames = [];
                for (int i = 0;
                    i < _schoolAdminController.studentItemsSelected.length;
                    i++) {
                  var student = _schoolAdminController.studentItemsSelected[i];
                  if (dto.userNames != null &&
                      !dto.userNames!.contains(student.userName.toString())) {
                    dto.userNames!.add(student.userName.toString());
                  }
                }
                var response =
                    await _adminServiceProvider.createParentLetters(dto);
                if (response.statusCode == 200) {
                  ParentLetterResult parentletter =
                      parentLetterResultFromJson(response.bodyString);
                  if (parentletter.pdfUrl != null &&
                      parentletter.pdfUrl != '') {
                    if (GetPlatform.isWeb || GetPlatform.isDesktop)
                      launchInWebViewWithJavaScript(
                          parentletter.pdfUrl.toString());
                    else
                      Get.to(() => PDFViewer(
                          pdfaddress: parentletter.pdfUrl.toString()));
                  }
                } else {
                  showSnackMsg('Failed to print parent letters');
                  // Get.snackbar(
                  //     'Message'.tr, 'Failed to print parent letters'.tr,
                  //     backgroundColor: Theme.of(context).primaryColor,
                  //     colorText: Theme.of(context).primaryColorLight,
                  //     snackPosition: SnackPosition.BOTTOM);
                }
              } else {
                showSnackMsg('Please choose at least one student');
                // Get.snackbar(
                //     'Message'.tr, 'Please choose at least one student'.tr,
                //     backgroundColor: Theme.of(context).primaryColor,
                //     colorText: Theme.of(context).primaryColorLight,
                //     snackPosition: SnackPosition.BOTTOM);
              }
            }),
      ],
    );
  }
}
